﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 图片裁剪压缩工具
{
    public class FreeCutEvent : IRectEvent
    {
        public int TargetWidth => 0;
        public int TargetHeight => 0; 

        public enum EnumMousePointPosition
        {
            MouseSizeNone = 0, //'无  
            MouseSizeRight = 1, //'拉伸右边框  
            MouseSizeLeft = 2, //'拉伸左边框  
            MouseSizeBottom = 3, //'拉伸下边框  
            MouseSizeTop = 4, //'拉伸上边框  
            MouseSizeTopLeft = 5, //'拉伸左上角  
            MouseSizeTopRight = 6, //'拉伸右上角  
            MouseSizeBottomLeft = 7, //'拉伸左下角  
            MouseSizeBottomRight = 8, //'拉伸右下角  
            MouseDrag = 9   // '鼠标拖动  
        }
        const int Band = 5;
        const int MinWidth = 10;
        const int MinHeight = 10;
        private EnumMousePointPosition m_MousePointPosition;
        private Point p;
        private bool isMouseDown = false;



        public void MouseUp(object? sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }
        public void MouseDown(object? sender, MouseEventArgs e)
        {
            p.X = e.X;
            p.Y = e.Y;
            isMouseDown = true;
        }

        public void MouseLeave(object? sender, EventArgs e)
        {
            var lCtrl = sender as RectView;
            m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;
            lCtrl.Cursor = Cursors.Arrow;
            isMouseDown = false;
        }

        public void MouseMove(object? sender, MouseEventArgs e)
        {
            var lCtrl = (sender as RectView);

            if (e.Button == MouseButtons.Left)
            {
                if (isMouseDown)
                {
                    switch (m_MousePointPosition)
                    {
                        case EnumMousePointPosition.MouseDrag://拖曳
                            lCtrl.Left = lCtrl.Left + e.X - p.X;
                            lCtrl.Top = lCtrl.Top + e.Y - p.Y;
                            break;
                        case EnumMousePointPosition.MouseSizeBottom:
                            lCtrl.Height = lCtrl.Height + e.Y - p.Y;
                            break;
                        case EnumMousePointPosition.MouseSizeBottomRight:
                            lCtrl.Width = lCtrl.Width + e.X - p.X;
                            lCtrl.Height = lCtrl.Height + e.Y - p.Y;
                            break;
                        case EnumMousePointPosition.MouseSizeRight:
                            lCtrl.Width = lCtrl.Width + e.X - p.X;
                            break;
                        case EnumMousePointPosition.MouseSizeTop:
                            lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
                            lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
                            break;
                        case EnumMousePointPosition.MouseSizeLeft:
                            lCtrl.Left = lCtrl.Left + e.X - p.X;
                            lCtrl.Width = lCtrl.Width - (e.X - p.X);
                            break;
                        case EnumMousePointPosition.MouseSizeBottomLeft:
                            lCtrl.Left = lCtrl.Left + e.X - p.X;
                            lCtrl.Width = lCtrl.Width - (e.X - p.X);
                            lCtrl.Height = lCtrl.Height + e.Y - p.Y;
                            break;
                        case EnumMousePointPosition.MouseSizeTopRight:
                            lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
                            lCtrl.Width = lCtrl.Width + (e.X - p.X);
                            lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
                            break;
                        case EnumMousePointPosition.MouseSizeTopLeft:
                            lCtrl.Left = lCtrl.Left + e.X - p.X;
                            lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
                            lCtrl.Width = lCtrl.Width - (e.X - p.X);
                            lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
                            break;
                        default:
                            break;
                    }
                    p = e.Location;
                    if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth;
                    if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight;
                    lCtrl. NotifPaint();
                }
            }
            else
            {
                m_MousePointPosition = MousePointPosition(lCtrl.Location,lCtrl.Size, e);   //'判断光标的位置状态  
                switch (m_MousePointPosition)   //'改变光标  
                {
                    case EnumMousePointPosition.MouseSizeNone:
                        lCtrl.Cursor = Cursors.Arrow;        //'箭头  
                        break;
                    case EnumMousePointPosition.MouseDrag:
                        //this.Cursor = Cursors.SizeAll;      //'四方向  
                        lCtrl.Cursor = Cursors.Arrow;      //'使用箭头样式
                        break;
                    case EnumMousePointPosition.MouseSizeBottom:
                        lCtrl.Cursor = Cursors.SizeNS;       //'南北  
                        break;
                    case EnumMousePointPosition.MouseSizeTop:
                        lCtrl.Cursor = Cursors.SizeNS;       //'南北  
                        break;
                    case EnumMousePointPosition.MouseSizeLeft:
                        lCtrl.Cursor = Cursors.SizeWE;       //'东西  
                        break;
                    case EnumMousePointPosition.MouseSizeRight:
                        lCtrl.Cursor = Cursors.SizeWE;       //'东西  
                        break;
                    case EnumMousePointPosition.MouseSizeBottomLeft:
                        lCtrl.Cursor = Cursors.SizeNESW;     //'东北到南西  
                        break;
                    case EnumMousePointPosition.MouseSizeBottomRight:
                        lCtrl.Cursor = Cursors.SizeNWSE;     //'东南到西北  
                        break;
                    case EnumMousePointPosition.MouseSizeTopLeft:
                        lCtrl.Cursor = Cursors.SizeNWSE;     //'东南到西北  
                        break;
                    case EnumMousePointPosition.MouseSizeTopRight:
                        lCtrl.Cursor = Cursors.SizeNESW;     //'东北到南西  
                        break;
                    default:
                        break;
                }
            }
        }
        private EnumMousePointPosition MousePointPosition(PointF viewLocation, SizeF size, MouseEventArgs e)
        {
            PointF point = new PointF(e.X - viewLocation.X, e.Y - viewLocation.Y);
            // Debug.WriteLine("test point:" + point);
            if ((point.X >= -1 * Band) | (point.X <= size.Width) | (point.Y >= -1 * Band) | (point.Y <= size.Height))
            {
                if (point.X < Band)
                {
                    if (point.Y < Band) { return EnumMousePointPosition.MouseSizeTopLeft; }
                    else
                    {
                        if (point.Y > -1 * Band + size.Height)
                        { return EnumMousePointPosition.MouseSizeBottomLeft; }
                        else
                        { return EnumMousePointPosition.MouseSizeLeft; }
                    }
                }
                else
                {
                    if (point.X > -1 * Band + size.Width)
                    {
                        if (point.Y < Band)
                        { return EnumMousePointPosition.MouseSizeTopRight; }
                        else
                        {
                            if (point.Y > -1 * Band + size.Height)
                            { return EnumMousePointPosition.MouseSizeBottomRight; }
                            else
                            { return EnumMousePointPosition.MouseSizeRight; }
                        }
                    }
                    else
                    {
                        if (point.Y < Band)
                        { return EnumMousePointPosition.MouseSizeTop; }
                        else
                        {
                            if (point.Y > -1 * Band + size.Height)
                            { return EnumMousePointPosition.MouseSizeBottom; }
                            else
                            { return EnumMousePointPosition.MouseDrag; }
                        }
                    }
                }
            }
            else
            { return EnumMousePointPosition.MouseSizeNone; }
        }

    }
}
